import 'package:flutter/material.dart';
import '../model/category.dart';
import '../service/service_method.dart';
import 'dart:convert';
import '../model/category_result.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../provide/child_category.dart';
import '../model/category_product.dart';
import '../provide/category_product_provide.dart';
class CartgoryPage extends StatefulWidget {
  @override
  _CartgoryPageState createState() => _CartgoryPageState();
}

class _CartgoryPageState extends State<CartgoryPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('分类'),
      ),
      body: Container(
        child: Row(
          children: <Widget>[
            LeftCategoryNav(),
            Expanded(child: Column(
              children: <Widget>[
                RightCategoryNav(),
                CategoryGoodsList(),
              ],
            ))

          ],
        ),
      ),
    );
  }


}

class LeftCategoryNav extends StatefulWidget {
  @override
  _LeftCategoryNavState createState() => _LeftCategoryNavState();
}

class _LeftCategoryNavState extends State<LeftCategoryNav> {

  List<Data> navList=[];

  var listIndex = 0;



  Widget _getNavItem(int index){
    return InkWell(
      onTap: (){
        var childList = navList[index].bxMallSubDto;
        Provide.value<ChildCategory>(context).getChildCategory(childList);
        setState(() {
          listIndex = index;
        });

      },
      child: Container(
        height: ScreenUtil().setHeight(100),
        padding: EdgeInsets.only(left: 10.0),
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          color: index ==listIndex? Color.fromRGBO
            (236, 236,236,  1.0) : Colors.white,
          border: Border(
            bottom: BorderSide(
              width: 1.0,
              color: Colors.black12
            )
          ),
        ),
        child: Text(navList[index].mallCategoryName,style: TextStyle(
          fontSize: ScreenUtil().setSp(28)
        ),),
      ),
    );
  }

  @override
  void initState() {
    _getCategory();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(180),
      decoration: BoxDecoration(
        border: Border(
          right: BorderSide(
            width: 1.0,
            color: Colors.black12
          )
        )
      ),
      child: ListView.builder(
          itemCount: navList.length,
          itemBuilder: (context,index){
        return _getNavItem(index);
      }),
    );
  }

  void _getCategory()async{
    await request('getCategory').then((value){
      var data= json.decode(value.toString());
      CategoryResult categoryResult = CategoryResult.fromJson(data);
      Provide.value<ChildCategory>(context).getChildCategory(categoryResult.data[0].bxMallSubDto);
      setState(() {
        navList = categoryResult.data;


      });
    });
  }


  void _getGoodsList(String categoryId)async{
    var data={
      'categoryId':categoryId == null ? '4' :categoryId,
      'categorySubId':'',
      'page':1
    };

    await request('getMallGoods',formData: data).then((val){
      var data = json.decode(val.toString());

      Category_Product category_product = Category_Product.
      fromJson(data);
      setState(() {
//        list = category_product.data;
      });
    });
  }
}


 
class RightCategoryNav extends StatefulWidget {
  @override
  _RightCategoryNavState createState() => _RightCategoryNavState();
}

class _RightCategoryNavState extends State<RightCategoryNav> {

//  final List list = ["名酒1", "名酒2","名酒3","名酒4","名酒5",
//  "名酒6","名酒7","名酒8","名酒9","名酒10","名酒11"];

  Widget _getRightItem(BxMallSubDto item){
    return InkWell(
      onTap: (){

      },
      child: Container(
        padding: EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
        child: Text(item.mallSubName,
        style: TextStyle(
          fontSize: ScreenUtil().setSp(28)
        ),),


      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(80),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            width: 1.0,color: Colors.black12,
          )
        )
      ),
      child: Provide<ChildCategory>(builder: (context,child,childcategoy){
        return ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: childcategoy.childCategoryList.length,
            itemBuilder: (context,index){
              return _getRightItem(childcategoy.childCategoryList[index]);
            });
      })
    );
  }
}

//商品列表，上拉加载 下啦刷新
class CategoryGoodsList extends StatefulWidget {
  @override
  _CategoryGoodsListState createState() => _CategoryGoodsListState();
}

class _CategoryGoodsListState extends State<CategoryGoodsList> {

  List<Product> list =[];

  @override
  void initState() {
    //_getGoodsList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return Container(
      child: Expanded(child: ListView.builder(
          itemCount: list.length,
          itemBuilder: (context,index){
            return _getItem(index);
          }),)
    );

  }



  Widget _goodsImage(index){
    return Container(
      width: ScreenUtil().setWidth(200),
      child: Image.network(list[index].image),
    );
  }

  Widget _goodsName(index){
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.all(5.0),
      child:Text(list[index].goodsName,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
            fontSize: ScreenUtil().setSp(28)
        ),)
    );
  }

  Widget _goodsPrices(index){
    return Container(
      margin: EdgeInsets.only(top: 20),
      child: Row(
        children: <Widget>[
          Text(
            '价格：¥${list[index].presentPrice}',
            style: TextStyle(
              color: Colors.pink,
              fontSize: ScreenUtil().setSp(30)
            ),
          ),
          SizedBox(
            width: 10,
          ),
          Text(
              '¥${list[index].oriPrice}',
            style: TextStyle(

              color: Colors.black26,
              fontSize: ScreenUtil().setSp(20),
              decoration: TextDecoration.lineThrough
            ),
          ),
        ],
      ),
    );
  }

  Widget _getItem(index){
    return InkWell(
      onTap: (){

      },
      child: Container(
        padding: EdgeInsets.only(top: 5.0,bottom: 5.0),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(
              width: 1.0,
              color: Colors.black12
            )
          )
        ),
        child: Row(
          children: <Widget>[
            _goodsImage(index),
            Expanded(
              child: Column(
                children: <Widget>[
                  _goodsName(index),
                  _goodsPrices(index),
                ],
              ),
            )

          ],
        ),
      ),
    );
  }
}

